package cn.aiszs.javafx_imp.controller;

import cn.aiszs.javafx_imp.pojo.City;
import cn.aiszs.javafx_imp.pojo.Ip;
import cn.aiszs.javafx_imp.pojo.Oil;
import cn.aiszs.javafx_imp.service.CityService;
import cn.aiszs.javafx_imp.service.IPService;
import cn.aiszs.javafx_imp.service.OilService;
import cn.aiszs.javafx_imp.util.HttpClient;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sun.glass.ui.monocle.util.C;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * 主要写一些事件调用的方法
 */

@FXMLController
public class MainController implements Initializable {

    @Autowired
    private CityService cityService;
    @Autowired
    private City city;

    @Autowired
    private OilService oilService;

    @Autowired
    private Oil oil;

    @Autowired
    private Ip ip;

    @Autowired
    private IPService ipService;

    public Button button;
    public Label label1;
    public Label label2;
    public Label label3;
    public Label label4;
    public Label label5;
    public Label label6;
    public TextField textField;
    public TextField textField2;
    public Button button2;
    public Label labeln1;
    public Label labeln2;
    public Label labeln3;
    public Label labeln4;
    public Label labeln5;
    public Label labeln6;

    public TextField textField1;
    public Button button1;
    public Label label10;
    public Label label11;
    public Label label12;


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //初始化
    }

    public void onBtnClick(){
        String province = textField.getText();
        String url = "https://qqlykm.cn/api/oilprice/get?key=BwFnOHiCptbeQv8pETzFE0VBQg&name=" + province;

        //是否在数据库中存在
        if(oilService.is_exist(province)){
            oil = oilService.selectByProvince(province);//从数据库中读取
        }else {
            String httpResult = HttpClient.sendRequest(url,"GET");//从接口中读取
            JSONObject jsonObject = JSON.parseObject(httpResult);
            if (jsonObject.getBoolean("success")){
                oil = oilService.insertData(httpResult);//写入数据库
            }else{
                oil.setProvince(province);
                oil.setUpdatetime(null);
                oil.setP0(null);
                oil.setP89(null);
                oil.setP95(null);
                oil.setP92(null);
            }
        }
        label1.setText("省份：" + oil.getProvince());
        label2.setText("p0：" + oil.getP0());
        label3.setText("p89：" + oil.getP89());
        label4.setText("P92：" + oil.getP92());
        label5.setText("p95：" + oil.getP95());
        label6.setText("update_time：" + oil.getUpdatetime());
    }
    
    /**手机归属地查询*/
    public void isBtnClick() {
        String phone = textField2.getText();
        String url = "https://qqlykm.cn/api/free/phone/get?key=RXyV0rZmfuyoz5U2MHnS86sayn&phone=" + phone;
        //是否在数据库中存在
        if (cityService.ciExist(phone)) {
            city = cityService.selectByPhone(phone);//从数据库中读取
        } else {
            String httpResult = HttpClient.sendRequest(url, "GET");//从接口中读取
            JSONObject jsonObject = JSON.parseObject(httpResult);
            if (jsonObject.getBoolean("success")) {
                city = cityService.inserphon(httpResult, phone);//写入数据库
            } else {
                city.setPhone(phone);
                city.setProvince(null);
                city.setCity(null);
                city.setAeracode(null);
                city.setPostcode(null);
                city.setIsp(null);
            }
        }
            labeln1.setText("手机号码" + city.getPhone());
            labeln2.setText("省份:" + city.getProvince());
            labeln3.setText("城市：" + city.getCity());
            labeln4.setText("地区代码：" + city.getAeracode());
            labeln5.setText("邮政代码" + city.getPostcode());
            labeln6.setText("运营商" + city.getIsp());
        }


    public void onBtn1Click(){
        String ipName = textField1.getText();
        String url = "https://qqlykm.cn/api/free/ip/get?key=iVm6n0H9VDSXmCeWj1BryLwALg&ip=" + ipName;

        //是否在数据库中存在
        if(ipService.is_exist(ipName)){
            ip = ipService.selectByIp(ipName);//从数据库中读取
            label10.setText("查询结果："+"查询成功。");
        }
        else {
            String httpResult = HttpClient.sendRequest(url,"GET");//从接口中读取
            JSONObject jsonObject = JSON.parseObject(httpResult);
            if (jsonObject.getBoolean("success")){
                ipService.insertData(httpResult);//写入数据库
                ip = ipService.selectByIp(ipName);
                label10.setText("查询结果："+"查询成功。");
            }else{
                label10.setText("查询结果："+"查询失败。");
            }
        }
        label11.setText("ip：" + ip.getIpIp());
        label12.setText("地址：" + ip.getIpAddress());
    }

}
